- add simple row estimation mechanism, allows preparing heavy queries better :)
authorDomas Mituzas <midom@users.mediawiki.org>
Sat, 7 Apr 2007 07:35:54 +0000 (07:35 +0000)
committerDomas Mituzas <midom@users.mediawiki.org>
Sat, 7 Apr 2007 07:35:54 +0000 (07:35 +0000)
- use estimates instead of select count(*) for jobs in Special:Statistics, won't scan million-row-tables too often

includes/Database.php
includes/SpecialStatistics.php

index 9af9135..6b7014f 100644 (file)
@@ -1124,6 +1124,10 @@ class Database {
                        $sql = $this->limitResult($sql, $options['LIMIT'],
                                isset($options['OFFSET']) ? $options['OFFSET'] : false);
                $sql = "$sql $postLimitTail";
+               
+               if (isset($options['EXPLAIN'])) {
+                       $sql = 'EXPLAIN ' . $sql;
+               }
 
                return $this->query( $sql, $fname );
        }
@@ -1156,6 +1160,33 @@ class Database {
                return $obj;
 
        }
+       
+       /**
+        * Estimate rows in dataset
+        * Returns estimated count, based on EXPLAIN output
+        * Takes same arguments as Database::select()
+        */
+       
+       function estimateRowCount( $table, $vars='*', $conds='', $fname = 'Database::estimateRowCount', $options = array() ) {
+               $options['EXPLAIN']=true;
+               $res = $this->select ($table, $vars, $conds, $fname, $options );
+               if ( $res === false )
+                       return false;
+               if (!$this->numRows($res)) {
+                       $this->freeResult($res);
+                       return 0;
+               }
+               
+               $rows=1;
+       
+               while( $plan = $this->fetchObject( $res ) ) {
+                       $rows *= ($plan->rows > 0)?$plan->rows:1; // avoid resetting to zero
+               }
+               
+               $this->freeResult($res);
+               return $rows;           
+       }
+       
 
        /**
         * Removes most variables from an SQL query and replaces them with X or N for numbers.
index 59cca3e..1c9e0ab 100644 (file)
@@ -23,7 +23,7 @@ function wfSpecialStatistics() {
        $users = SiteStats::users();
 
        $admins = $dbr->selectField( 'user_groups', 'COUNT(*)', array( 'ug_group' => 'sysop' ), $fname );
-       $numJobs = $dbr->selectField( 'job', 'COUNT(*)', '', $fname );
+       $numJobs = $dbr->estimateRowCount('job');
 
        if ($action == 'raw') {
                $wgOut->disable();